package de.tum.in.eist.poll.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.tum.in.eist.poll.pub.shared.Lecturer;
import de.tum.in.eist.poll.pub.shared.User;

/**
 * This class creates the Login-Interface.
 */

public class LoginView extends HorizontalPanel {
	private TextBox userName = new TextBox();
	private PasswordTextBox password = new PasswordTextBox();
	private Button loginButton = new Button();
	private DialogBox message = new DialogBox();

	
	/**
	 * Generates a panel for logging in.
 	 */	
	public LoginView(){
		userName.setText("User");
		password.setText("Password");
		loginButton.setText("Login");
		userName.setTitle("Please enter your username.");
		password.setTitle("Please enter your password.");
		

		userName.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if(userName.getText().equals("User"))
				userName.setText("");
			}
		});
		
		password.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if(password.getText().equals("Password"))
				password.setText("");
			}
		});
		
		loginButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				AsyncCallback<User> callback = new AsyncCallback<User>() {
					public void onFailure(Throwable caught) {
						message.setText("ERROR");
						message.setModal(true);
						VerticalPanel content = new VerticalPanel();
						Label text = new Label();
						Button ok = new Button();
						ok.setText("OK");
						ok.addClickHandler(new ClickHandler() {
							
							@Override
							public void onClick(ClickEvent event) {
								message.hide();
								
							}
						});
						text.setText(caught.getMessage());
						content.add(text);
						content.add(ok);
						message.setWidget(content);
						message.center();
					}

					public void onSuccess(User result) {
						
						RootPanel root = RootPanel.get("main");
						root.clear();
						EIST_POLL.currentUser = result;
						if (result instanceof Lecturer){
							root.add(new AdminView());
						}
						else{
							AsyncCallback<Integer> callback = new AsyncCallback<Integer>() {

								@Override
								public void onFailure(Throwable caught) {
									EIST_POLL.getNotifications();
								}

								@Override
								public void onSuccess(Integer result) {
									EIST_POLL.lastNotificationId = result;
									EIST_POLL.getNotifications();
								}
							};
							EIST_POLL.notificationService.getLastID(callback);
							root.add(new SelectLectureView());
						}
						
						// logged in view
						RootPanel login = RootPanel.get("login");
						login.clear();
						login.add(new LoggedInView());
					}
				};

				EIST_POLL.service.login(userName.getText(), password.getText(), callback);
			}
		});
		
		password.addKeyPressHandler(new KeyPressHandler() {
			
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if(event.getCharCode() == KeyCodes.KEY_ENTER)
					loginButton.click();
			}
		});
		
		userName.setFocus(true);
		
		this.add(userName);
		this.add(password);
		this.add(loginButton);
	}
}
